package com.atguigu.gmall.common.config.exception;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ClassName: GmallGlobleExceptionHandler
 * Package: com.atguigu.gmall.common.config.exception
 * Description:
 *
 * @Author ltystart
 * @Create 2025/8/28 17:54
 * @Version 1.0
 */
//@ControllerAdvice//这是一个异常处理器
//@ResponseBody
@RestControllerAdvice//这是一个异常处理器，出现任何异常，由他负责处理
public class GmallGlobleExceptionHandler {

    /**
     * 处理业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler(GmallException.class)
    public Result handleBizException(GmallException ex){
        Integer code = ex.getCode();
        String message = ex.getMessage();
        Result<Object> fail = Result.fail();
        fail.setCode( code);
        fail.setMessage( message);
        return fail;
    }

    /**
     * 处理其他系统运行期间异常
     * @param throwable
     * @return
     */
    @ExceptionHandler(Throwable.class)
    public Result handleSysException(Throwable throwable){
        Result<Object> fail = Result.fail();
        fail.setMessage( throwable.getMessage());
        return fail;
    }
}
